package com.gton.lpl.common.diyerror;


import com.gton.lpl.common.pojo.Resp;
import com.mysql.jdbc.exceptions.MySQLSyntaxErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @description: 全局异常处理
 * @author: GuoTong
 * @createTime: 2021-06-28 16:22
 * @since JDK 1.8 OR 11
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHander {

    @ExceptionHandler(value = Exception.class)
    public Resp exceptionHandler(Exception e) {
        e.printStackTrace();
        return Resp.error(e.getMessage());
    }

    @ExceptionHandler(value = MyLoginException.class)
    public Resp MyLoginException(Exception e) {
        log.error("用户未登录");
        String message = e.getMessage();
        int NO_LOGIN_CODE = 403;
        if (String.valueOf(NO_LOGIN_CODE).equals(message)) {
            return Resp.Ok(e.getMessage());
        }
        return Resp.error(e.getMessage());
    }

    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public Resp msgNotFind(Exception e) {
        e.printStackTrace();
        log.error("请求错误");
        String message = e.getMessage();
        return Resp.error("请求内容未传递" + message);
    }

    @ExceptionHandler(value = MySQLSyntaxErrorException.class)
    public Resp msgMySQLExecuteError(Exception e) {
        e.printStackTrace();
        log.error("Mysql执行异常");
        String message = e.getMessage();
        return Resp.error(message);
    }


}
